home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / rename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.8 KB  |  74 lines

  1. /*
  2.  *    rename() - rename a file
  3.  */
  4. #include "lib.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <signal.h>
  8. #include <errno.h>
  9.  
  10. #define    PUBLIC
  11.  
  12. #ifndef    NULL
  13. #define NULL    (void *) 0
  14. #endif
  15.  
  16. extern int    errno;
  17.  
  18. /*========================================================================*\
  19. **                rename()                  **
  20. \*========================================================================*/
  21. PUBLIC int
  22. rename( from, to )
  23. _CONST char *from, *to;
  24. {
  25. /*
  26.  *    Attempts to link 'from' as 'to'.  If 'to' exists it is unlinked.
  27.  *
  28.  *    NOTE:      'rename()' will not rename across file systems or 
  29.  *        file types.  Also, if an attempt is made to copy across
  30.  *        file systems both files will be left intact and an
  31.  *        error will be returned.
  32.  */
  33.   struct stat    s_to, s_from;
  34.   void        (*s_int)(), (*s_hup)(), (*s_quit)();
  35.   int        ret = 0;
  36.   
  37.   /*
  38.    *    get status if 'from' and 'to', if either attemp fails we know 
  39.    *    one of the files doesn't exist.  if 'from' doesn't exist then
  40.    *    return an error condition.  if both files exist then test if
  41.    *    their both on the same file system.  if 'to' doesn't exist then
  42.    *    don't worry about it, it soon will.
  43.    */
  44.   if (stat( from, &s_from ) == 0) {
  45.         if (stat( to, &s_to ) == 0) {
  46.             if ( s_to.st_dev  == s_from.st_dev ) {
  47.                 errno = EXDEV;
  48.                 return( -1 );
  49.             }
  50.         }
  51.         /*
  52.          *    Ignore SIGINT, SIGHUP, and SIGQUIT until 
  53.          *    we'er finished.
  54.          */
  55.         s_int  = signal( SIGINT,  SIG_IGN );
  56.         s_hup  = signal( SIGHUP,  SIG_IGN );
  57.         s_quit = signal( SIGQUIT, SIG_IGN );  
  58.         /*    
  59.          *    does 'to' exist? if so remove it
  60.          */
  61.         ret = unlink( to );
  62.         if ((ret = link( from, to )) == 0) 
  63.             ret = unlink( from );
  64.         /*
  65.          *    Restore signals
  66.          */
  67.         signal( SIGINT,  s_int  );
  68.         signal( SIGHUP,  s_hup  );
  69.         signal( SIGQUIT, s_quit );
  70.         return( ret );
  71.   } else
  72.     return( -1 );
  73. }
  74.